package com.infra.micro.oss.domain.service.impl;

import com.infra.common.resp.KeyValue;
import com.infra.micro.oss.domain.db.entity.BizInfoPO;
import com.infra.micro.oss.domain.db.entity.FileInfoPO;
import com.infra.micro.oss.domain.service.DownloadService;
import com.infra.micro.oss.model.vo.BizFileVO;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DownloadServiceImpl extends BaseFileService implements DownloadService {
    @Override
    public void downloadByBizId(Long bizId, HttpServletResponse response) throws Exception {
        BizInfoPO bizInfoPO = bizInfoRepository.queryByBizId(bizId);
        if (bizInfoPO == null) {
            new IllegalArgumentException("业务ID无效，文件不存在!");
        }

        downloadByFileId(bizInfoPO.getFileId(), bizInfoPO.getFileName(), response);
    }

    @Override
    public void downloadByFileId(Long fileId, HttpServletResponse response) throws Exception {
        downloadByFileId(fileId, fileId.toString(), response);
    }

    @Override
    public void downloadByFileId(Long fileId, String fileName, HttpServletResponse response) throws Exception {
        FileInfoPO fileInfoPO = fileInfoRepository.queryByFileId(fileId);
        if (fileInfoPO == null) {
            new IllegalArgumentException("业务ID无效，文件不存在!");
        }
        downloadFile(fileId, fileName, fileInfoPO.getMimeType(), response);
    }

    private void downloadFile(Long fileId, String fileName, String contentType, HttpServletResponse response) throws Exception {
        response.setHeader("content-type", contentType);
        response.setContentType(contentType);
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
        writeOutputStream(ossClient.getFileObject(fileId.toString()), response);
    }

    @Override
    public void downloadByBizIds(List<Long> bizIds, HttpServletResponse response) throws Exception {
        List<BizFileVO> bizFileVOList = bizInfoRepository.queryByBizIds(bizIds);
        List<KeyValue> list = bizFileVOList.stream().map(item -> new KeyValue(item.getFileId().toString(), item.getFileName())).collect(Collectors.toList());
        downloadZip(String.format("oss-biz-file-f%s", LocalDate.now().toString()), list, response);
    }

    @Override
    public void downloadByFileIds(List<Long> fileIds, HttpServletResponse response) throws Exception {
        List<FileInfoPO> fileInfoPOS = fileInfoRepository.queryByFileIds(fileIds);
        List<KeyValue> list = fileInfoPOS.stream().map(item -> new KeyValue(item.getFileId().toString(), item.getFileId().toString())).collect(Collectors.toList());
        downloadZip(String.format("oss-sys-file-f%s", LocalDate.now().toString()), list, response);
    }

    private void downloadZip(String fileName, List<KeyValue> list, HttpServletResponse response) throws Exception {
        response.setHeader("content-type", "application/x-zip-compressed");
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName + ".zip", "UTF-8"));
        writeZipOutputStream(list, response);
    }
}
